package com.example.demo.controller;

import com.example.autojob.util.message.MessageManager;
import com.example.demo.entity.TimingDataEntity;
import com.example.demo.entity.TimingDataEntityCacheData;
import com.example.demo.repository.TimingDataEntityRepository;
import com.timing.finder.framework.HandleType;
import com.timing.finder.framework.TimeSeriesDataFinder;
import com.timing.finder.framework.TimingFinderApplication;
import com.timing.finder.framework.TimingFinderBootstrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author JingGe(* ^ ▽ ^ *)
 * @date 2023-07-17 16:02
 * @email 1158055613@qq.com
 */
@RestController
@RequestMapping("timing_db")
public class TimingDataController {
    private final TimeSeriesDataFinder<TimingDataEntity> finder = new TimeSeriesDataFinder<>("demo");

    @GetMapping(value = "/index", produces = "application/json;charset=UTF-8")
    public String index(@RequestParam(value = "DATE", required = false) String date, @RequestParam(value = "KEY", required = false) String keyPattern, @RequestParam("USE_PATTERN") Boolean usePattern) {
        boolean isPattern = usePattern != null && usePattern;
        if (isPattern) {
            return MessageManager.getJsonMessage(MessageManager.Code.OK, "查找成功", finder.indexPattern(keyPattern, date, TimingDataEntityCacheData.class));
        }
        return MessageManager.getJsonMessage(MessageManager.Code.OK, "查找成功", finder.index(keyPattern, date, TimingDataEntityCacheData.class, true));
    }

    @GetMapping(value = "/range", produces = "application/json;charset=UTF-8")
    public String pattern(@RequestParam(value = "START", required = false) String start, @RequestParam("END") String end, @RequestParam(value = "KEY", required = false) String keyPattern, @RequestParam("USE_PATTERN") Boolean usePattern) {
        boolean isPattern = usePattern != null && usePattern;
        if (isPattern) {
            return MessageManager.getJsonMessage(MessageManager.Code.OK, "查找成功", finder.rangePattern(keyPattern, start, end, TimingDataEntityCacheData.class));
        }
        return MessageManager.getJsonMessage(MessageManager.Code.OK, "查找成功", finder.range(keyPattern, start, end, true, TimingDataEntityCacheData.class));
    }
}
